package com.seed.core.query;

import com.mybatisflex.core.query.QueryCondition;
import com.seed.core.pojo.Join;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.pojo.Searched;

import java.util.*;

public class SeedDataPermied {

    private final List<List<List<List<QueryCondition>>>> wheres = new ArrayList<>();
    private final Map<String, Join> joins = new LinkedHashMap<>(); // 表关联对象
    private final Map<String, SeedInCondition> inQuerys = new LinkedHashMap<>();
    private final List<List<List<Map<String, Searched>>>> fillDataPermied = new ArrayList<>(); // fill对象使用

    public SeedDataPermied(Seed selfSeed, List<List<List<Map<String, Searched>>>> dataPermieds) {
        for (List<List<Map<String, Searched>>> listList: dataPermieds) {
            List<List<List<QueryCondition>>> wherelistlistlist = new ArrayList<>();
            List<List<Map<String, Searched>>> fillSearchedListList = new ArrayList<>();
            for (List<Map<String, Searched>> list: listList) {
                List<List<QueryCondition>> wherelistlist = new ArrayList<>();
                List<Map<String, Searched>> fillSearchedList = new ArrayList<>();
                for (Map<String, Searched> searcheds: list) {
                    SeedSearched seedSearched = new SeedSearched(selfSeed, searcheds);
                    joins.putAll(seedSearched.getJoins());
                    inQuerys.putAll(seedSearched.getInQuerys());
                    fillSearchedList.add(seedSearched.getFillSearcheds());

                    List<QueryCondition> wherelist = new ArrayList<>();
                    if(!seedSearched.getWheres().isEmpty()) {
                        wherelist.add(seedSearched.getWheres().get(0));
                    } else if(!seedSearched.getInQuerys().isEmpty()) {
                        wherelist.add(seedSearched.getInQuerys().get(0).getQueryCondition());
                    } else {
                        wherelist.add(QueryCondition.createEmpty());
                    }
                    wherelistlist.add(wherelist);
                }
                wherelistlistlist.add(wherelistlist);
                fillSearchedListList.add(fillSearchedList);
            }
            wheres.add(wherelistlistlist);
            fillDataPermied.add(fillSearchedListList);
        }
    }

    public QueryCondition getQueryCondition() {
        QueryCondition ret = QueryCondition.createEmpty();
        for(List<List<List<QueryCondition>>> listlistlist: wheres) {
            QueryCondition finalCondition = QueryCondition.createEmpty();
            for(List<List<QueryCondition>> listlist: listlistlist) {
                QueryCondition haveCondition = QueryCondition.createEmpty();
                for(List<QueryCondition> list: listlist) {
                    QueryCondition selfCondition = QueryCondition.createEmpty();
                    for(QueryCondition condition: list) selfCondition.and(condition);
                    haveCondition.or(selfCondition);
                }
                finalCondition.and(haveCondition);
            }
            ret.or(finalCondition);
        }

        return ret;
    }

    public List<List<List<List<QueryCondition>>>> getWheres() {
        return wheres;
    }

    public Map<String, Join> getJoins() {
        return joins;
    }

    public Map<String, SeedInCondition> getInQuerys() {
        return inQuerys;
    }

    public List<List<List<Map<String, Searched>>>> getFillDataPermied() {
        return fillDataPermied;
    }
}
